﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Business.Repository;
using Rio.Applications.Web.Services.BusinessEntities;
using Rio.Doc.Business.Repository;
#endregion

namespace Rio.Applications.Web.Services
{
    /// <summary>
    /// Class to handle Business entities in a RIA services
    /// </summary>
    public partial class BusinessEntitiesDomainService : DomainService
    {

        /// <summary>
        /// Add an alert
        /// </summary>
        /// <param name="alerteName">the name ofthe alert</param>
        /// <param name="alerteObject">The object to include in the alert</param>
        /// <param name="alerteBody">the body of the alert</param>
        /// <param name="alerteDate">the date when the alert must be sent</param>
        /// <param name="folderIdFolderTypeId">The folder id folder type id.</param>
        /// <param name="groupIds">The group ids.</param>
        /// <param name="action">The action.</param>
        /// <returns>
        /// The database identifier of the new alert
        /// </returns>
        [Invoke(HasSideEffects=true)]
        public IEnumerable<ReturnLong> AddAlert(string alerteName, string alerteObject, string alerteBody, DateTime alerteDate, Dictionary<long, long> folderIdFolderTypeId, List<long> groupIds, int? action)
        {
            var l_oReturnLongs = new List<ReturnLong>();
            User l_oUser = GetUser();
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(alerteName<" + alerteName + ">alerteObject<" + alerteObject + ">alerteBody<" + alerteBody + ">alerteDate<" + alerteDate + ">p_dicFolderTypeIdFolderId.Count<" + folderIdFolderTypeId.Count + ">");
                var l_oReturnLong = AlertRepository.AddAlert(alerteName, alerteObject, alerteBody, alerteDate, l_oUser.UserId, folderIdFolderTypeId, groupIds, action);
                l_oReturnLongs.Add(l_oReturnLong);
            }
            catch (Exception ex)
            {
                var l_oReturnLong = new ReturnLong(("(alerteName<" + alerteName + ">alerteObject<" + alerteObject + ">alerteBody<" + alerteBody + ">alerteDate<" + alerteDate + ">"), ex);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in AddAlert : " + l_oReturnLong.FullErrorMessage);
                l_oReturnLongs.Add(l_oReturnLong);
            }

            return l_oReturnLongs;
        }


        /// <summary>
        /// Get all alerts of today
        /// </summary>
        /// <returns> The database identifier of the new alert</returns>
        [Query]
        public IEnumerable<Alert> GetAllAlertOfToday()
        {
            var l_oRet = new List<Alert>();
            
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(No Parameters)...");
                l_oRet = AlertRepository.GetAllAlert();
            }
            catch (Exception ex)
            {
                var l_oAlert = new Alert("(No parameters....)", ex);
                l_oRet.Add(l_oAlert);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in GetAllAlertOfToday : " + l_oAlert.FullErrorMessage);
            }
            return l_oRet;
        }


        /// <summary>
        /// Get all alerts of today
        /// </summary>
        /// <param name="p_lFolderTypeId">The identifier of the foldertpe</param>
        /// <param name="p_lFolderId">The identifier of the folder</param>
        /// <returns> The database alerts that ocurs today for a folder</returns>
        [Query]
        public IEnumerable<Alert> GetAllAlertOfTodayForAFolder(long p_lFolderTypeId, long p_lFolderId)
        {
            var l_oRet = new List<Alert>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("AddFolder(p_lFolderTypeId<" + p_lFolderTypeId + "> p_lFolderId<" + p_lFolderId + ">");
                l_oRet = AlertRepository.GetAllAlertOfAFolder(p_lFolderTypeId, p_lFolderId);
            }
            catch (Exception ex)
            {
                var l_oAlert = new Alert("AddFolder(p_lFolderTypeId<" + p_lFolderTypeId + "> p_lFolderId<" + p_lFolderId + ">", ex);
                l_oRet.Add(l_oAlert);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in GetAllAlertOfTodayForAFolder : " + l_oAlert.FullErrorMessage);
            }


            return l_oRet;
        }



        /// <summary>
        /// Add an alert for a list of users
        /// </summary>
        /// <param name="p_lstGroupIds">The list of all groups identifiers</param>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        /// <returns>A ReturnBoolean Object containing all necessary informations</returns>
        [Query(HasSideEffects=true)]
        public IEnumerable<ReturnBoolean> AddAlertForAGroup(List<long> p_lstGroupIds, long p_lAlertId)
        {
            var l_oRet = new List<ReturnBoolean>();
            
            var l_oReturnBoolean = new ReturnBoolean();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("AddFolder(groupIds.Count<" + p_lstGroupIds.Count + "> p_lAlertId<" + p_lAlertId + ">");
                AlertRepository.AddAlertForAGroup(p_lstGroupIds, p_lAlertId);
                l_oRet.Add(l_oReturnBoolean);
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("AddFolder(groupIds.Count<" + p_lstGroupIds.Count + "> p_lAlertId<" + p_lAlertId + ">)", ex);
                l_oRet.Add(l_oReturnBoolean);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in AddAlertForAGroup : " + l_oReturnBoolean.FullErrorMessage);
            }

            return l_oRet;
        }

        /// <summary>
        /// Update an alert
        /// </summary>
        /// <param name="p_dicAlertIdStatusId">A dictionnary containing foreach alert identifier, the new status</param>
        /// <returns>A ReturnBoolean Object containing all necessary informations</returns>
        [Invoke(HasSideEffects=true)]
        public IEnumerable<ReturnBoolean> UpdateAlert(Dictionary<long, long> p_dicAlertIdStatusId)
        {
            var l_oRet = new List<ReturnBoolean>();
            var l_oReturnBoolean = new ReturnBoolean();
            try
            {
                User l_oUser = GetUser();
                NetBay.Core.Log.LogManager.Instance().LogDebug("AddFolder(p_dicAlertIdStatusId.Count<" + p_dicAlertIdStatusId.Count + ">");
                AlertRepository.UpdateAlert(p_dicAlertIdStatusId, l_oUser.UserId);
                l_oRet.Add(l_oReturnBoolean);
            }
            catch (Exception ex)
            {
                l_oReturnBoolean = new ReturnBoolean("AddFolder(p_dicAlertIdStatusId.Count<" + p_dicAlertIdStatusId.Count + ">)", ex);
                l_oRet.Add(l_oReturnBoolean);
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in UpdateAlert : " + l_oReturnBoolean.FullErrorMessage);
            }
            
            return l_oRet;
        }

    }
}